<template>
    <div>
        <el-button @click="addCategory()">添加类目</el-button>
        <!-- 显示类目列表 -->
        <div class="table_container">
            <el-table :data="tableData" border style="width: 90%">
                <el-table-column label="类目名" prop="course_name" />
                <el-table-column>
                    <template #header>
                        图片
                    </template>
                    <template #default="scope">
                        <el-image style="width: 100px; height: 100px" :src="scope.row.coures_img" fit="cover" />
                    </template>
                </el-table-column>
                <el-table-column label="描述" prop="description" />
                <el-table-column label="创建时间" prop="create_time" />
                <el-table-column align="right">
                    <template #header>
                        <el-input @keyup.enter="handleSearch" v-model="search" size="small" placeholder="请输入想要查询的类目" />
                    </template>
                    <template #default="scope">
                        <el-button size="small" @click="handleAddList(scope.$index, scope.row)">
                            添加列表数据
                        </el-button>
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>




        <!-- 添加类目 -->
        <el-dialog v-model="dialogFormVisible" title="添加类目" width="500">
            <el-form>
                <el-form-item label="类目名称">
                    <el-input v-model="name" autocomplete="off" />
                </el-form-item>
                <el-form-item label="类目描述">
                    <el-input v-model="description" autocomplete="off" />
                </el-form-item>
                <el-form-item label="类目图片">
                    <el-input v-model="img" autocomplete="off" />
                </el-form-item>

            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="clickOK">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>




<script setup>
import { $_add_course, $_get_courses, $_delete_courses } from "@/api/user.js";
import { ref, computed } from "vue";
import { commonMessage } from "@/utils/index";
import { formatDate } from "@/utils/date.js";
import { useRouter } from "vue-router";
const router = useRouter()
const dialogFormVisible = ref(false)
const name = ref('')
const description = ref('')
const img = ref('')
const page = ref(1)
const pageSize = ref(10)
const keyword = ref('')
const search = ref('')
const tableData = ref([])
const course_id = ref('')

const addCategory = () => {
    dialogFormVisible.value = true
}
const clickOK = () => {
    dialogFormVisible.value = false
    if (course_id.value) {
        $_add_course({ course_name: name.value, description: description.value, coures_img: img.value, course_id: course_id.value })
            .then(res => {
                if (res.success) {
                    course_id.value = ''
                    commonMessage(res.msg)
                    get_courses()
                }
            })
    } else {
        $_add_course({ course_name: name.value, description: description.value, coures_img: img.value })
            .then(res => {
                if (res.success) {
                    commonMessage(res.msg)
                    get_courses()
                }
            })
    }

}

const get_courses = (data) => {
    $_get_courses({
        page: page.value,
        pageSize: pageSize.value,
        keyword: keyword.value,
    }).then(res => {
        for (const iterator of res.result) {
            iterator.create_time = formatDate(iterator.create_time)
        }
        tableData.value = res.result
    })
}
get_courses()

const handleEdit = (index, row) => {
    dialogFormVisible.value = true
    name.value = row.course_name
    description.value = row.description
    img.value = row.coures_img
    course_id.value = row.course_id
}
const handleDelete = (index, row) => {
    ElMessageBox.confirm(
        '请确认是否删除?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            $_delete_courses({
                course_id: row.course_id
            }).then(res => {
                if (res.success) {
                    page.value = 1
                    pageSize.value = 10
                    keyword.value = ''
                    get_courses()
                    commonMessage(res.msg)
                }
            })
        })

}
const handleAddList = (index, row) => {
    router.push({
        path: '/topicList',
        query: {
            course_id: row.course_id,
            course_name: row.course_name
        }
    })
}
const handleSearch = () =>{
    page.value=1,
    pageSize.value=10,
    keyword.value=search.value
    get_courses()
}
</script>

<style lang="scss" scoped>
.table_container {
    width: 100%;
    display: flex;
    justify-content: center;
}
</style>